﻿namespace HuaWei
{
    using HuaWei.DAI;
    using HuaWei.DALFactory;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.IO;
    using System.Runtime.InteropServices;

    public class WCFService : IWCFService
    {
        private const string _PRONO = "JLSoft#FACEbook.61646#*((899";

        public WCFService()
        {
            this.InitUseDog();
        }

        public void BeginTrans()
        {
        }

        private bool CheckDogExist()
        {
            return true;
        }

        public void CloseDatabase()
        {
        }

        public void Commit()
        {
        }

        public DataSet ExecuteDataSet(string productCode, string exeStr, DbParameter[] outParams)
        {
            IDataAccess bSDAL = this.GetBSDAL(productCode);
            bSDAL.OpenDatabase();
            DataSet set = bSDAL.ExecuteDataSet(exeStr, outParams);
            bSDAL.CloseDatabase();
            return set;
        }

        public DataSet ExecuteDataSet(string productCode, string exeStr, CommandType cmdType)
        {
            IDataAccess bSDAL = this.GetBSDAL(productCode);
            bSDAL.OpenDatabase();
            DataSet set = bSDAL.ExecuteDataSet(exeStr, cmdType);
            bSDAL.CloseDatabase();
            return set;
        }

        public DataSet ExecuteDataSet(string productCode, string exeStr, DbParameter[] outParams, string TableName)
        {
            IDataAccess bSDAL = this.GetBSDAL(productCode);
            bSDAL.OpenDatabase();
            DataSet set = bSDAL.ExecuteDataSet(exeStr, outParams, TableName);
            bSDAL.CloseDatabase();
            return set;
        }

        public DataSet ExecuteDataSet(string productCode, string exeStr, CommandType cmdType, DbParameter[] inParams)
        {
            IDataAccess bSDAL = this.GetBSDAL(productCode);
            bSDAL.OpenDatabase();
            DataSet set = bSDAL.ExecuteDataSet(exeStr, cmdType, inParams);
            bSDAL.CloseDatabase();
            return set;
        }

        public DataSet ExecuteDataSet(string productCode, string exeStr, CommandType cmdType, string TableName)
        {
            IDataAccess bSDAL = this.GetBSDAL(productCode);
            bSDAL.OpenDatabase();
            DataSet set = bSDAL.ExecuteDataSet(exeStr, cmdType, TableName);
            bSDAL.CloseDatabase();
            return set;
        }

        public DataSet ExecuteDataSet(string productCode, string exeStr, CommandType cmdType, DbParameter[] inParams, DbParameter[] outParams)
        {
            IDataAccess bSDAL = this.GetBSDAL(productCode);
            bSDAL.OpenDatabase();
            DataSet set = bSDAL.ExecuteDataSet(exeStr, cmdType, inParams, outParams);
            bSDAL.CloseDatabase();
            return set;
        }

        public DataSet ExecuteDataSet(string productCode, string exeStr, CommandType cmdType, DbParameter[] inParams, string TableName)
        {
            IDataAccess bSDAL = this.GetBSDAL(productCode);
            bSDAL.OpenDatabase();
            DataSet set = bSDAL.ExecuteDataSet(exeStr, cmdType, inParams, TableName);
            bSDAL.CloseDatabase();
            return set;
        }

        public DataSet ExecuteDataSet(string productCode, string exeStr, CommandType cmdType, DbParameter[] inParams, DbParameter[] outParams, string TableName)
        {
            IDataAccess bSDAL = this.GetBSDAL(productCode);
            bSDAL.OpenDatabase();
            DataSet set = bSDAL.ExecuteDataSet(exeStr, cmdType, inParams, outParams, TableName);
            bSDAL.CloseDatabase();
            return set;
        }

        public bool ExecuteNonQuery(string productCode, ArrayList sqls, CommandType cmdType)
        {
            IDataAccess bSDAL = this.GetBSDAL(productCode);
            bSDAL.OpenDatabase();
            bool flag = bSDAL.ExecuteNonQuery(sqls, cmdType);
            bSDAL.CloseDatabase();
            return flag;
        }

        public bool ExecuteNonQuery(string productCode, Hashtable sqls, CommandType cmdType)
        {
            IDataAccess bSDAL = this.GetBSDAL(productCode);
            bSDAL.OpenDatabase();
            bool flag = bSDAL.ExecuteNonQuery(sqls, cmdType);
            bSDAL.CloseDatabase();
            return flag;
        }

        public int ExecuteNonQuery(string productCode, string proName, DbParameter[] outParams)
        {
            IDataAccess bSDAL = this.GetBSDAL(productCode);
            bSDAL.OpenDatabase();
            int num = bSDAL.ExecuteNonQuery(proName, outParams);
            bSDAL.CloseDatabase();
            return num;
        }

        public int ExecuteNonQuery(string productCode, string exeStr, CommandType cmdType)
        {
            IDataAccess bSDAL = this.GetBSDAL(productCode);
            bSDAL.OpenDatabase();
            int num = bSDAL.ExecuteNonQuery(exeStr, cmdType);
            bSDAL.CloseDatabase();
            return num;
        }

        public int ExecuteNonQuery(string productCode, string exeStr, CommandType cmdType, DbParameter[] inParams)
        {
            IDataAccess bSDAL = this.GetBSDAL(productCode);
            bSDAL.OpenDatabase();
            int num = bSDAL.ExecuteNonQuery(exeStr, cmdType, inParams);
            bSDAL.CloseDatabase();
            return num;
        }

        public int ExecuteNonQuery(string productCode, string exeStr, CommandType cmdType, DbParameter[] inParams, DbParameter[] outParams)
        {
            IDataAccess bSDAL = this.GetBSDAL(productCode);
            bSDAL.OpenDatabase();
            int num = bSDAL.ExecuteNonQuery(exeStr, cmdType, inParams, outParams);
            bSDAL.CloseDatabase();
            return num;
        }

        public object ExecuteReader(string productCode, string exeStr, DbParameter[] outParams)
        {
            IDataAccess bSDAL = this.GetBSDAL(productCode);
            bSDAL.OpenDatabase();
            object obj2 = bSDAL.ExecuteReader(exeStr, outParams);
            bSDAL.CloseDatabase();
            return obj2;
        }

        public object ExecuteReader(string productCode, string exeStr, CommandType cmdType)
        {
            IDataAccess bSDAL = this.GetBSDAL(productCode);
            bSDAL.OpenDatabase();
            object obj2 = bSDAL.ExecuteReader(exeStr, cmdType);
            bSDAL.CloseDatabase();
            return obj2;
        }

        public object ExecuteReader(string productCode, string exeStr, CommandType cmdType, DbParameter[] inParams)
        {
            IDataAccess bSDAL = this.GetBSDAL(productCode);
            bSDAL.OpenDatabase();
            object obj2 = bSDAL.ExecuteReader(exeStr, cmdType, inParams);
            bSDAL.CloseDatabase();
            return obj2;
        }

        public object ExecuteReader(string productCode, string exeStr, CommandType cmdType, DbParameter[] inParams, DbParameter[] outParams)
        {
            IDataAccess bSDAL = this.GetBSDAL(productCode);
            bSDAL.OpenDatabase();
            object obj2 = bSDAL.ExecuteReader(exeStr, cmdType, inParams, outParams);
            bSDAL.CloseDatabase();
            return obj2;
        }

        public object ExecuteScalar(string productCode, string exeStr, DbParameter[] outParams)
        {
            IDataAccess bSDAL = this.GetBSDAL(productCode);
            bSDAL.OpenDatabase();
            object obj2 = bSDAL.ExecuteScalar(exeStr, outParams);
            bSDAL.CloseDatabase();
            return obj2;
        }

        public object ExecuteScalar(string productCode, string exeStr, CommandType cmdType)
        {
            IDataAccess bSDAL = this.GetBSDAL(productCode);
            bSDAL.OpenDatabase();
            object obj2 = bSDAL.ExecuteScalar(exeStr, cmdType);
            bSDAL.CloseDatabase();
            return obj2;
        }

        public object ExecuteScalar(string productCode, string exeStr, CommandType cmdType, DbParameter[] inParams)
        {
            IDataAccess bSDAL = this.GetBSDAL(productCode);
            bSDAL.OpenDatabase();
            object obj2 = bSDAL.ExecuteScalar(exeStr, cmdType, inParams);
            bSDAL.CloseDatabase();
            return obj2;
        }

        public object ExecuteScalar(string productCode, string exeStr, CommandType cmdType, DbParameter[] inParams, DbParameter[] outParams)
        {
            IDataAccess bSDAL = this.GetBSDAL(productCode);
            bSDAL.OpenDatabase();
            object obj2 = bSDAL.ExecuteScalar(exeStr, cmdType, inParams, outParams);
            bSDAL.CloseDatabase();
            return obj2;
        }

        private IDataAccess GetBSDAL(string productCode)
        {
            if (productCode.Equals("JLSoft#FACEbook.61646#*((899"))
            {
                string filename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "System.ini");
                string datasource = IniFile.ReadIniFile(filename, "Sys", "Address", false);
                string username = IniFile.ReadIniFile(filename, "Sys", "DBUser", false);
                string dbname = IniFile.ReadIniFile(filename, "Sys", "DBName", false);
                string password = "aa";
                if (datasource.Equals(""))
                {
                    datasource = ".";
                    password = "aa";
                }
                if (username.Equals(""))
                {
                    username = "sa";
                }
                if (username.Equals("qidao"))
                {
                    password = "szewec!@#$";
                }
                else if (datasource.Equals("10.1.30.146"))
                {
                    password = ".netdb";
                }
                return CDALFactory.GetDALAccess("MSSQL", datasource, dbname, username, password);
            }
            return null;
        }

        public DataSet GetDs(string productNO, DataSet dsSource, string invokeCode, string param)
        {
            if ("JLSoft#FACEbook.61646#*((899".Equals(productNO))
            {
               InvokeManager manager = new InvokeManager();
                manager.SetIDA(this.GetBSDAL(productNO));
                return manager.GetDs(dsSource, invokeCode, param);
            }
            return null;
        }

        private void InitUseDog()
        {
        }

        public string Invoke(string productNO, DataSet dsSource, string invokeCode, string param)
        {
            if ("JLSoft#FACEbook.61646#*((899".Equals(productNO))
            {
                InvokeManager manager = new InvokeManager();
                manager.SetIDA(this.GetBSDAL(productNO));
                return manager.Invoke(dsSource, invokeCode, param);
            }
            return "ERROR";
        }

        public bool Login(string productNO, string loginName, string password)
        {
            return productNO.Equals("JLSoft#FACEbook.61646#*((899");
        }

        public bool Login(string productNO, string loginName, string password, out string serverDbType)
        {
            serverDbType = "";
            return productNO.Equals("JLSoft#FACEbook.61646#*((899");
        }

        public void OpenDatabase()
        {
        }

        public void RollBack()
        {
        }

        public string SyncData(string productCode, DataSet dsSource)
        {
            string tableName = "";
            string str2 = "";
            string str3 = "";
            string str4 = "";
            string exeStr = "";
            string str6 = "";
            DataTableCollection tables = dsSource.Tables;
            IDataAccess bSDAL = this.GetBSDAL(productCode);
            DataSet dsUpdate = new DataSet();
            bSDAL.OpenDatabase();
            try
            {
                bSDAL.BeginTrans();
                try
                {
                    foreach (DataTable table in tables)
                    {
                        str4 = "";
                        tableName = table.TableName;
                        if (tableName.Equals("ProcData"))
                        {
                            ArrayList sqls = new ArrayList();
                            string str7 = "";
                            foreach (DataRow row in table.Rows)
                            {
                                tableName = row["BM"].ToString();
                                str7 = row["RecId"].ToString();
                                str6 = "delete from " + tableName + " where id = '" + str7 + "';";
                                sqls.Add(str6);
                            }
                            bSDAL.ExecuteNonQuery(sqls, CommandType.Text);
                        }
                        else if (table.Rows.Count > 0)
                        {
                            if (table.Columns["bdid"] != null)
                            {
                                str3 = table.Rows[0]["bdid"].ToString();
                            }
                            if (table.Columns["xmid"] != null)
                            {
                                str2 = table.Rows[0]["xmid"].ToString();
                            }
                            foreach (DataRow row in table.Rows)
                            {
                                if (str4 != "")
                                {
                                    str4 = str4 + ",";
                                }
                                object obj2 = str4;
                                str4 = string.Concat(new object[] { obj2, "'", row["id"], "'" });
                            }
                            exeStr = "select * from " + tableName + " where 1=2";
                            str6 = "delete from " + tableName + " where id in (" + str4 + ")";
                            if (str2.Equals("xmid"))
                            {
                                exeStr = exeStr + " and xmid = '" + str2 + "'";
                            }
                            if (str2.Equals("bdid"))
                            {
                                exeStr = exeStr + " and bdid = '" + str3 + "'";
                            }
                            bSDAL.ExecuteNonQuery(str6, CommandType.Text);
                            dsUpdate.Merge(bSDAL.ExecuteDataSet(exeStr, CommandType.Text, tableName));
                            DataTable table2 = dsUpdate.Tables[tableName];
                            foreach (DataRow row in table.Rows)
                            {
                                table2.Rows.Add(row.ItemArray);
                            }
                        }
                    }
                    bSDAL.Update(dsUpdate, false);
                    bSDAL.Commit();
                }
                catch (Exception exception)
                {
                    bSDAL.RollBack();
                    return ("Error:" + exception.Message);
                }
            }
            finally
            {
                bSDAL.CloseDatabase();
            }
            return "";
        }

        public void Update(string productCode, DataTable table)
        {
            IDataAccess bSDAL = this.GetBSDAL(productCode);
            bSDAL.OpenDatabase();
            bSDAL.Update(table);
            bSDAL.CloseDatabase();
        }

        public void Update(string productCode, DataSet dsUpdate, [Optional, DefaultParameterValue(true)] bool writeLog)
        {
            IDataAccess bSDAL = this.GetBSDAL(productCode);
            bSDAL.OpenDatabase();
            bSDAL.Update(dsUpdate, writeLog);
            bSDAL.CloseDatabase();
        }

        public void Update(string productCode, DataSet dsUpdate, string strTableName)
        {
            IDataAccess bSDAL = this.GetBSDAL(productCode);
            bSDAL.OpenDatabase();
            bSDAL.Update(dsUpdate, strTableName);
            bSDAL.CloseDatabase();
        }

        public void Update(string productCode, DataSet dsChanges, Dictionary<DataRowState, string[]> updateIndex, params string[] exeStr)
        {
            IDataAccess bSDAL = this.GetBSDAL(productCode);
            bSDAL.OpenDatabase();
            bSDAL.Update(dsChanges, updateIndex, exeStr);
            bSDAL.CloseDatabase();
        }

        public string ConnectionString
        {
            get
            {
                return "";
            }
            set
            {
            }
        }

        public ConnectionState ConnState
        {
            get
            {
                return ConnectionState.Open;
            }
        }

        public string DBType
        {
            get
            {
                return "";
            }
            set
            {
            }
        }
    }
}

